package com.yl.demo.utils.db;

import org.apache.logging.log4j.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库连接
 */
public class DBUtil {
    private final static Logger logger = LoggerFactory.getLogger(DBUtil.class);

    private static String driverName;

    private static String address;

    private static String name;

    private static String pwd;

    /**
     * 获取数据库连接属性
     */
    public DBUtil() {
        {
            Properties properties = new Properties();
            try {
                InputStream in = new PropertiesUtil(properties).getClass().getResourceAsStream("/application.properties");
                properties.load(in);
            } catch (Exception e) {
                logger.error("获取properties的方法异常，{}", e.getMessage());
            }
            /**获取配置选项*/
            this.driverName = properties.getProperty("spring.datasource.driver-class-name");
            this.address = properties.getProperty("spring.datasource.url");
            this.name = properties.getProperty("spring.datasource.username");
            this.pwd = properties.getProperty("spring.datasource.password");
        }
        /**驱动注册*/
        {
            try {
                Class.forName(driverName);
            } catch (Exception e) {
                logger.error("驱动注册异常，{}", e.getMessage());
            }
        }
    }


    /**
     * 获取 Connetion
     *
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(address, name, pwd);
        } catch (Exception e) {
            logger.error("获取 Connetion异常，{}", e.getMessage());
        }
        return connection;
    }

    /**
     * 执行查询sql
     *
     * @param con
     * @param sql
     * @return
     */
    public static PreparedStatement getPStatement(Connection con, String sql) {
        try {
            con = getConnection();
            PreparedStatement statement = con.prepareStatement(sql);
            return statement;
        } catch (SQLException e) {
            logger.error("执行查询sql异常，{}", e.getMessage());
        }
        return null;
    }

    /**
     * 关闭资源
     */
    public static void close(Connection con) {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static void close(Connection con, PreparedStatement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (con != null && !con.isClosed()) {
                con.close();
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null && !con.isClosed()) {
                con.close();
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}
